package com.weizhizs.assistant.code.template.data;

import com.weizhizs.assistant.code.config.ParamConfig;
import com.weizhizs.assistant.code.helper.TableHelper;
import com.weizhizs.assistant.code.template.IDataLoador;
import com.weizhizs.assistant.code.vo.TableVO;
import com.weizhizs.assistant.utils.DateUtils;
import com.weizhizs.assistant.utils.StringUtils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class MenuSqlData implements IDataLoador {

    @Override
    public String getTemplateName(TableVO tableVO) {
        return ParamConfig.getInstance().getTemplatePath() + "/menu.sql.vm";
    }

    @Override
    public String getFileName(String tableName) {
        return "mysql" + File.separator + tableName.toLowerCase() + "_menu.sql";
    }

    @Override
    public String getFileOutPath() {
        return "";
    }

    @Override
    public Map<String, Object> getDataMap(TableVO tableVO) {
        String tableName = tableVO.getTableName();
        String tableComment = tableVO.getTableComment();

        //封装模板数据
        String clazzName = TableHelper.tableNameToClassName(tableName);
        String instanceName = StringUtils.firstToLowerCase(clazzName);

        Map<String, Object> map = new HashMap<>();
        map.put("tableName", tableName);
        map.put("comments", tableComment);
        map.put("pk", tableVO.getPk());
        map.put("className", clazzName);
        map.put("classname", instanceName);
        map.put("pathName", TableHelper.pathFromTableName(tableName));
        map.put("filePath", TableHelper.filepathFromTableName(tableName));
        map.put("allColumns", tableVO.getAllColumns());
        map.put("columns", TableHelper.getRealColumns(tableVO.getAllColumns(), ParamConfig.getInstance().getEntityLocalFields()));
        map.put("moduleName", TableHelper.moduleFromTableName(tableName));

        map.put("authorName", ParamConfig.getInstance().getAuthorName());
        map.put("version", ParamConfig.getInstance().getVersion());
        map.put("currentDate", DateUtils.nowDateString());

        return map;
    }

}
